/*
 * @(#)ContactOrganizationTransformer.java
 * Date 2013-08-15
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.pix.transformer;

import java.util.List;

import javax.enterprise.context.ApplicationScoped;

import javax.inject.Inject;

import org.hl7.v3.COCTMT150003UV03Organization;
import org.hl7.v3.COCTMT150007UVOrganization;
import org.hl7.v3.ON;

import us.hornerscorners.lamppost.exception.ServiceActionException;

import us.hornerscorners.lamppost.hl7v3common.utils.HL7Utils;

import us.hornerscorners.lamppost.model.patient.ContactOrganization;

import us.hornerscorners.lamppost.transformer.AbstractTransformer;


/**
 *
 * @author jim
 */
@ApplicationScoped
public class ContactOrganizationTransformer
        extends AbstractTransformer<COCTMT150003UV03Organization, ContactOrganization> {

    /** Field description */
    @Inject
    private ContactPartyTransformer contactPartyTransformer;

    /** Field description */
    @Inject
    private InstanceIdentifierTransformer instanceIdentifierTransformer;

    /**
     * Constructs ...
     *
     */
    public ContactOrganizationTransformer() {
        super();
    }

    /**
     * Method description
     *
     *
     * @param prpaOrg
     *
     * @return
     */
    @Override
    public ContactOrganization transform(COCTMT150003UV03Organization prpaOrg)
            throws ServiceActionException {

        ContactOrganization result = null;

        if (prpaOrg != null) {

            result = new ContactOrganization();

            result.getIdentifiers().addAll(
                this.instanceIdentifierTransformer.transform(prpaOrg.getId()));

            List<ON> names = prpaOrg.getName();

            if (names.isEmpty() == false) {

                result.setName(
                    HL7Utils.parseFirstSerializable(names.get(0).getContent()));
            }

            result.getContactParties().addAll(
                this.contactPartyTransformer.transform(prpaOrg.getContactParty()));
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param prpaOrg
     *
     * @return
     */
    public ContactOrganization transform(COCTMT150007UVOrganization prpaOrg)
            throws ServiceActionException {

        ContactOrganization result = null;

        if (prpaOrg != null) {

            result = new ContactOrganization();

            result.getIdentifiers().addAll(
                this.instanceIdentifierTransformer.transform(prpaOrg.getId()));

            List<ON> names = prpaOrg.getName();

            if (names.isEmpty() == false) {

                result.setName(
                    HL7Utils.parseFirstSerializable(names.get(0).getContent()));
            }

            result.getContactParties().addAll(
                this.contactPartyTransformer.transformCOCTMT150007UVContactParty(
                    prpaOrg.getContactParty()));
        }

        return result;
    }
}
